package com.macrog.rabbitmq.topic;

import com.rabbitmq.client.*;
import com.macrog.rabbitmq.utils.ConnectionFactoryUtil;

import java.io.IOException;

/**
 * Created by Administrator on 2018/9/2.
 */
public class Recv0 {

    private static final String EXCHANGE_NAME = "topicExchange";
    private static final String QUEUEQ_NAME = "topicQueue1";

    private static final String[] bindingKeys = {"key.hhh", "key.hhh.abc", "abc.ttt.uuu", "abc.uuu"};

    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionFactoryUtil.getConnection();
        final Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUEQ_NAME, false, false, false, null);

//        channel.queueBind(QUEUEQ_NAME,EXCHANGE_NAME,"key.*");
//        channel.queueBind(QUEUEQ_NAME,EXCHANGE_NAME,"abc.#");

        //binding那些bindingKey
        //可以单个单个设置，也可以批量设置
        channel.queueBind(QUEUEQ_NAME, EXCHANGE_NAME, "key.*");
        channel.queueBind(QUEUEQ_NAME, EXCHANGE_NAME, "abc.#");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者111：" + new String(body, "UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };

        channel.basicConsume(QUEUEQ_NAME, false, consumer);
    }
}
